// pages/video/video.js
import {allChannels,getArticles} from '../../api/news'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    flag:false,
    shuju:[],
    channels:[],
    canshu:{
      channel_id:0,
      timestamp:null
    }
    
  },
  onClick(event) {
     this.setData({
     "canshu.channel_id": event.detail.name
     })
     if(event.detail.name>=3){
      this.setData({
        "canshu.channel_id": event.detail.name+1,
        })
     }
    
    this.getArticle()

  },
 async channels(){
const {data} = await allChannels()
this.setData({
  channels:data.data.channels
})
this.selectComponent('#tabs').resize()
  },
  async getArticle(){
    const {data} = await getArticles(this.data.canshu)
    this.setData({
      shuju:data.data.results
    })

  },
  bindrefresherpulling(e){
    if(e.detail.dy>45) {
      this.setData({
        refresher:'释放即可刷新...'
      })
    } else {
      this.setData({
        refresher:'下拉即可刷新...'
      })
    }
  },
 // 触底//
async chudi(){
  this.setData({
    "canshu.timestamp": Date.parse(new Date())
  })
  const {data} = await getArticles(this.data.canshu)
  const a = data.data.results
  this.setData({
    shuju:[...this.data.shuju,...a]
  })
},
ap(e){
console.log(e);
},
 async addd(){
    this.setData({
      refresher:'加载中...'
    })
    try {
      this.setData({
        "canshu.timestamp": Date.parse(new Date())
      })
      const {data} = await getArticles(this.data.canshu)
      const a = data.data.results
      this.setData({
        shuju:[...a,...this.data.shuju]
      })
      this.setData({
        refresher:'刷新成功',
      })
    } catch (error) {
      this.setData({
        refresher:'刷新失败，稍后重试',
      })
    }
    setTimeout(() => {
     this.setData({
      flag:false
     })
    },1000)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      "canshu.timestamp": Date.parse(new Date())
    })
    this.channels()
    this.getArticle()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})